﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Manage_ProjectUpdate : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            if (!IsPostBack)
            {
                string ProjectId, strHospitalName = "";
                int i = 0;
                if (Request.QueryString["ProjectId"] != null)
                {
                    ProjectId = SysFun.IsSQL(Request.QueryString["ProjectId"].ToString());
                    txtProjectId.Text = ProjectId;
                    txtProjectId.Visible = false;
                }
                else
                {
                    return;
                }

                Db DbProject = new Db();
                System.Data.IDataReader dr;
                string strSql = "select accountid,attorneyname from Attorney order by attorneyname";
                dr = DbProject.DataReader(strSql);
                while (dr.Read())
                {
                    lstAccount.Items.Add(dr["attorneyname"].ToString());
                    lstAccount.Items[i++].Value = dr["accountid"].ToString();
                }
                dr.Close();

                strSql = "select projectid,ProjectMarker,Title,Content,ProjectHabitus,Hospitals,AccountId from Project where ProjectId=" + ProjectId.ToString();
                dr = DbProject.DataReader(strSql);
                if (dr.Read())
                {
                    txtProjectMarker.Text = dr["ProjectMarker"].ToString();
                    txtTitle.Text = dr["Title"].ToString();
                    txtContent.Text = dr["Content"].ToString();
                    txtProjectHabitus.Text = dr["ProjectHabitus"].ToString();

                    strHospitalName = dr["Hospitals"].ToString();
                    lstAccount.SelectedValue = dr["AccountId"].ToString();
                }
                dr.Close();

                strSql = "select accountid,hospitalname from hospital where accountid not in (" + strHospitalName + ") order by hospitalname";
                dr = DbProject.DataReader(strSql);
                i = 0;
                while (dr.Read())
                {
                    Hospitals2.Items.Add(dr["hospitalname"].ToString());
                    Hospitals2.Items[i++].Value = dr["accountid"].ToString();
                }
                dr.Close();
                strSql = "select accountid,hospitalname from hospital where accountid  in (" + strHospitalName + ") order by hospitalname";
                dr = DbProject.DataReader(strSql);
                i = 0;
                while (dr.Read())
                {
                    Hospitals1.Items.Add(dr["hospitalname"].ToString());
                    Hospitals1.Items[i++].Value = dr["accountid"].ToString();
                }
                dr.Close();
                dr.Dispose();
                DbProject.Close();


            }
        }
    }
    protected void butSave_Click(object sender, EventArgs e)
    {
        int i;
        try
        {
            if (Hospitals1.Items.Count== 0)
            {
                Response.Write("<script>alert('医院列表不能为空！！！');</script>");
            }
            else
            {

                string strHospitalid = "'";
                for (i = 0; i < Hospitals1.Items.Count; i++)
                    strHospitalid = strHospitalid + Hospitals1.Items[i].Value + "','";
                strHospitalid = strHospitalid.Substring(0, strHospitalid.Length - 2);
                Db db = new Db();
                Grove.ORM.ObjectOperator oo = db.GetObjectOperator();

                HZ.MBSM.Model.Project Project = new HZ.MBSM.Model.Project();
                Project.ProjectId = Convert.ToInt32(txtProjectId.Text);
                Project.ProjectMarker = txtProjectMarker.Text;
                Project.Title = txtTitle.Text;
                Project.Content = txtContent.Text;
                Project.AccountId = Convert.ToInt64(lstAccount.SelectedValue);
                Project.Hospitals = strHospitalid;
                Project.ProjectHabitus = "0";
                oo.Update(Project);
                oo.Dispose();
                db.Close();
                Response.Write("<script>alert('保存成功！！！');</script>");
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "Project", Project.Title + "|" + Project.Content + "|" + Project.AccountId + "|" + Project.ProjectId + "|" + Project.ProjectMarker + "|" + Project.Hospitals, "../Magage/ProjectUpdate.aspx");

                Response.Write("<script>window.opener.location.reload();window.close();</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }
    protected void butAdd_Click(object sender, EventArgs e)
    {
        int j, i;
        j = 0;
        for (i = 0; i < Hospitals2.Items.Count; i++)
        {
            if (Hospitals2.Items[i].Selected == true)
            {
                Hospitals1.Items.Add(Hospitals2.Items[i]);                
            }
        }
        for (i = 0; i < Hospitals2.Items.Count; i++)
        {
            for (j = 0; j < Hospitals1.Items.Count; j++)
                if (Hospitals2.Items.Count > 0)
                {

                    {
                        if (Hospitals2.Items[i].Value == Hospitals1.Items[j].Value)
                            Hospitals2.Items.Remove(Hospitals2.Items[i]);
                    }
                }
        }

    }
    protected void butDel_Click(object sender, EventArgs e)
    {
        int j, i;
        j = 0;
        for (i = 0; i < Hospitals1.Items.Count; i++)
        {
            if (Hospitals1.Items[i].Selected == true)
            {
                Hospitals2.Items.Add(Hospitals1.Items[i]);
            }
        }
        for (i = 0; i < Hospitals1.Items.Count; i++)
        {
            for (j = 0; j < Hospitals2.Items.Count; j++)
                if (Hospitals1.Items.Count > 0)
                {

                    {
                        if (Hospitals1.Items[i].Value == Hospitals2.Items[j].Value)
                            Hospitals1.Items.Remove(Hospitals1.Items[i]);
                    }
                }
        }

    }
}
